package com.utils.catche;


import org.apache.commons.beanutils.PropertyUtils;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.Collection;

/**
 * 列表工具类，基于apache的CollectionUtils
 *
 * @author: dingjiejun
 * @version: 1.0
 * @date: 2017/3/23
 */
public class CollectionUtils extends org.apache.commons.collections.CollectionUtils {


    private static Object getValue(Object record, String valueProperty) {
        Method getter;
        PropertyDescriptor descriptor;
        try {
            descriptor = PropertyUtils.getPropertyDescriptor(record, valueProperty);
            getter = descriptor.getReadMethod();
        } catch (Exception e) {
            return null;
        }
        if (getter == null) {
            throw new RuntimeException();
        }
        Object value;
        try {
            value = getter.invoke(record);
        } catch (Exception e) {
            return null;
        }
        return value;
    }

    /**
     * 将指定属性连接为字符串
     *
     * @param records
     * @param valueProperty
     * @param separator
     * @return
     */
    public static String concat(Collection<?> records, String valueProperty, String separator) {
        if (isEmpty(records)) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        boolean next = false;
        for (Object record : records) {
            Object value = getValue(record, valueProperty);
            if (next) {
                sb.append(separator);
            } else {
                next = true;
            }
            sb.append(value == null ? "" : String.valueOf(value));
        }

        return sb.toString();
    }

    /**
     * 将整个连接为字符串
     *
     * @param separator
     * @param records
     * @return
     */
    public static String concat(String separator, Collection<?> records) {
        if (isEmpty(records)) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        boolean next = false;
        for (Object record : records) {
            if (next) {
                sb.append(separator);
            } else {
                next = true;
            }
            sb.append(record.toString());
        }

        return sb.toString();
    }

    /**
     * 将指定属性转为数组
     *
     * @param records
     * @param valueProperty
     * @return
     */
    public static Object[] toArray(Collection<?> records, String valueProperty) {
        if (isEmpty(records)) {
            return null;
        }
        Object[] result = new Object[records.size()];
        int i = 0;
        for (Object record : records) {
            result[i++] = getValue(record, valueProperty);
        }
        return result;
    }

    /**
     * 将指定属性转为字符串数组
     *
     * @param records
     * @param valueProperty
     * @return
     */
    public static String[] toStringArray(Collection<?> records, String valueProperty) {
        if (isEmpty(records)) {
            return null;
        }
        String[] result = new String[records.size()];
        int i = 0;
        for (Object record : records) {
            Object value = getValue(record, valueProperty);
            result[i++] = (value == null ? null : value.toString());
        }
        return result;
    }

    /**
     * 将指定属性连接为字符串，逗号分隔
     *
     * @param records
     * @param valueProperty
     * @return
     */
    public static String concat(Collection<?> records, String valueProperty) {
        return concat(records, valueProperty, ",");
    }

    /**
     * 将整个连接为字符串，逗号分隔
     *
     * @param records
     * @return
     */
    public static String concat(Collection<?> records) {
        return concat(",", records);
    }


}

